//字面量类型
let msg: "hello world" = "hello world"  //必须值必须要与字面量类型一样
// msg = "hello worlD"  这样会报错
// 应用 可以省去switch操作
type Alignment = 'left' | 'right' | 'center'
function changeAlign(align: Alignment) { //定义参数只能在'left' | 'right' | 'center'中选择,其他为非法值
    console.log("修改方向", align);
}
changeAlign("left")
// changeAlign("left1") 输入非法值
export { }

//字面量推理
const info = {
    url: "https://coderwhy.org/abc",
    method: "GET"
}
function request(url: string, method: "GET" | "POST") {
    console.log(url, method);
}
// request(info.url,info.method)    会报错info.methods是string而不是字面量
//解决方法1
request(info.url, info.method as 'GET')
//解决方法2
{

    const info = {
        url: "https://coderwhy.org/abc",
        method: "GET"
    } as const  //进行字面量推理 as const是语法,const断言告诉编译器为表达式推断出它能推断出的最窄或最特定的类型

    function request(url: string, method: "GET" | "POST") {
        console.log(url, method);
    }
    request(info.url, info.method)
}
//解决方法3
{
    type Method = "GET" | "POST"
    type Request = {
        url: string,
        method: Method
    }
    const info:Request = {
        url: "https://coderwhy.org/abc",
        method: "GET"
    }

    function request(url: string, method: "GET" | "POST") {
        console.log(url, method);
    }
    request(info.url, info.method)
}